/**
 * 订单信息管理初始化
 */
var Order = {
    id: "OrderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Order.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '订单编号', field: 'orderNo', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'userName', visible: true, align: 'center', valign: 'middle'},
        {title: '客户手机号', field: 'userPhone', visible: true, align: 'center', valign: 'middle'},
        {title: '地址', field: 'address', visible: true, align: 'center', valign: 'middle'},
        {title: '订单信息', field: 'orderInfos', visible: true, align: 'center', valign: 'middle'},
        {title: '订单总金额', field: 'amount', visible: true, align: 'center', valign: 'middle'},
        {title: '预付金额', field: 'depositAmount', visible: true, align: 'center', valign: 'middle'},
        {title: '尾款金额', field: 'remainBalance', visible: false, align: 'center', valign: 'middle'},
        {title: '销售员', field: 'salesName', visible: true, align: 'center', valign: 'middle'},
        {title: '销售日期', field: 'salesTime', visible: true, align: 'center', valign: 'middle',formatter:Order.dateFormat},
        {title: '安装员', field: 'setupName', visible: true, align: 'center', valign: 'middle'},
        {title: '安装日期', field: 'setupTime', visible: true, align: 'center', valign: 'middle',formatter:Order.dateFormat},
        {title: '备注信息', field: 'remarks', visible: false, align: 'center', valign: 'middle'},
        {title: '订单状态', field: 'statusName', visible: true, align: 'center', valign: 'middle'}
    ];
};


Order.dateFormat=function(value){
    console.log("value:"+value);
    if(value!=undefined){
        var date =  new Date(value);
        var y = 1900+date.getYear();
        var m = "0"+(date.getMonth()+1);
        var d = "0"+date.getDate();
        return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
    }else {
        return "";
    }

}

/**
 * 检查是否选中
 */
Order.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Order.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加订单信息
 */
Order.openAddOrder = function () {
    var index = layer.open({
        type: 2,
        title: '添加订单信息',
        area: ['1100px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/orderInfo/order_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看订单信息详情
 */
Order.openOrderDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '订单信息详情',
            area: ['1100px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/orderInfo/order_update/' + Order.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除订单信息
 */
Order.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/orderInfo/delete", function (data) {
            Feng.success("删除成功!");
            Order.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("orderId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询订单信息列表
 */
Order.search = function () {
    var queryData = {};
    queryData['userName'] = $("#userName").val();
    queryData['userPhone'] = $("#userPhone").val();
    queryData['address'] = $("#address").val();
    Order.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Order.initColumn();
    var table = new BSTable(Order.id, "/orderInfo/list", defaultColunms);
    table.setPaginationType("server");
    Order.table = table.init();
});
